const util = require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    users: wx.getStorageSync('USERS')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  formSubmit: function (e) {
    var that = this;
    util.showLoading();
    var parmas = {
      "uid": that.data.users.id,
      "teamName": e.detail.value.teamName,
      "teamDesc": e.detail.value.teamDesc
    };
    if (!that.data.users.id) {
      that.onLogin();
    }
    var interval = setInterval(function () {
      if (that.data.users.id) {
        clearInterval(interval);
        parmas.uid = that.data.users.id;
        that.onSaveTeams(parmas);
      }
    }, 1 * 10);
  },
  onSaveTeams: function (parmas) {//保存申请团队信息
    var that = this;
    if (parmas.uid && parmas.uid > 0 && parmas.teamName && parmas.teamName != "") {
      util.wxPost({
        url: "/rest/lufei/apply", params: parmas,
        success: function (res) {
          util.hideToast();
          if (res.data.status == "000") {
            var users = that.data.users;
            users.teamId = res.data.data.id;
            wx.setStorage({
              key: "USERS",
              data: users,
              success: function () {
                wx.showToast({
                  title: "申请团队成功",
                  icon: "success",
                  duration: 3000,
                  complete: function () {
                    wx.clearStorageSync();
                    wx.setStorageSync("USERSClean", "1");
                    wx.switchTab({
                      url: '/pages/index/index'
                    });
                  }
                });
              }
            });
          } else {
            wx.showToast({
              title: "申请团队失败!",
              icon: "failure",
              duration: 1500
            });
          }
        }
      });
    } else {
      wx.showToast({
        title: "团队名称不能为空!",
        icon: "failure",
        duration: 1500
      });
    }
  },
  onLogin: function (e) {//登录处理
    var that = this;
    var hasLogin = false;
    wx.checkSession({
      success: function (options) {
        console.log("checkSession:ok");
        if (options.errMsg == "checkSession:ok") {
          that.setData({
            users: wx.getStorageSync('USERS')
          });
          if (!that.data.users.id) {
            hasLogin = true;
          }
        }
      }
    });
    if (!hasLogin) {
      wx.login({
        success: function (options) {
          if (options.code) {
            util.wxPost({
              url: "/rest/lufei/login", params: { "code": options.code },
              success: function (data) {
                that.setData({
                  users: data.data.data
                });
                wx.setStorage({
                  key: "USERS",
                  data: data.data.data,
                });
              }
            });
          }
        },
        fail: function () {
          console.log("登录想睡觉了");
        }
      });
    }
  }
})